
module.exports = Card;

function Card(id){
	this._id = id;
	this._color = Math.floor(id / 13);
	this._value = id % 13;

	if( this._color==4 )
		this._value += 13;

}

var colors = new Array("黑桃", "红桃", "方片", "梅花", "");
var values = new Array("3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "小王", "大王");

Card.prototype.getID = function( ){
	return this._id;
}

Card.prototype.getValue = function( ){
	return this._value;
}

Card.prototype.getColor = function( ){
	return this._color;
}

Card.prototype.getName = function( ) {
	return colors[this._color] + values[this._value];
}

// 比较两张牌的大小
Card.compare = function(a,b) {

	if(a.getValue()<b.getValue())
		return -1;
	else 
		return 1;
}
Card.cardType = {
	danpai : 1,
	duizi : 2,
	sanzhang : 3,
	sandai1 : 4,
	sandai2 : 5,
	sidai2 : 6,
	zhadan : 7,
	shunzi : 8,
	liandui : 9,
	feiji : 10,
	wangzha : 11
}

Card.cardTypeName = ['错误','单牌','对子','3张','3带1','3带1对','4带2','炸弹','顺子','连队','飞机','王炸']

// 统计牌型数据
function getInfo(ls){
	// 将1,2,3,4张的牌分开
	var diff = [[],[],[],[]];
	var a = ls[0].getValue();
	var n = 0;			
	// 不同牌的数量
	var sum = 1;		

	for (var i = 1; i < ls.length; i++) {
		var v = ls[i].getValue();		
		if(a != v){
			diff[n].push(a);
			n = 0;
			sum ++;
		}
		else
			n++;
		a = v;
	}
	diff[n].push(a);
	info = [diff[0].length,diff[1].length,diff[2].length,diff[3].length];
	//返回统计信息
	return {'diff':diff, 'info':info, 'sum':sum};
}

var parse4 = function(ls){
	var len = ls.length;

	// ls 已经排过序
	if( ls[0].getValue()==ls[len-1].getValue() ){
		var c4 = [Card.cardType.danpai,Card.cardType.duizi,Card.cardType.sanzhang,Card.cardType.zhadan];
		return c4[len-1];
	}
	if( len==4 && (
		ls[0].getValue()==ls[2].getValue() ||
		ls[1].getValue()==ls[3].getValue()) )		
		return Card.cardType.sandai1;

	if( len==2 && ls[0].getColor() == 4 && ls[1].getColor() == 4)
		return Card.cardType.wangzha; // 王炸
	return;

}

var parseMuti = function(ls){
	//判断顺子
	function shunzi(ss){
		var x = ss[0]
		for (var i = 1; i < ss.length; i++){
			if(x+1 != ss[i])
				return;
			x = ss[i];
		}
		return Card.cardType.shunzi;
	}

	var xx = getInfo(ls);
	var diff = xx.diff
	var info = xx.info;

	if(xx.sum == ls.length)
		return shunzi(diff[0]);
	
	if(xx.sum == ls.length/2){
		if(shunzi(diff[1]))
			return Card.cardType.liandui;
	}

	if(xx.sum == 2){
		if(info[3] == 1)
			return Card.cardType.sidai2; 	// 4+2

		if(info[2] == 1 && info[1]==1)
			return Card.cardType.sandai2;	// 3+2
		
		if(info[2] == 2 && diff[0][0]+1 == diff[0][1])
			return Card.cardType.feiji;		// 飞机不带

		return;
	}

	if (diff[2].length >= 2 && shunzi(diff[2])!=null)
		return Card.cardType.feiji;			// 飞机
}

Card.parseCardType = function(ls) {
	if (ls.length==0) 
		return null;

	ls.sort(Card.compare)

	var typeId = 0;

	if(ls.length <= 4)
		typeId = parse4(ls)
	else
		typeId = parseMuti(ls);

	if (typeId!=null){
		console.log(Card.cardTypeName[typeId]);
		return {'typeId':typeId,'cards':ls}
	}
}

var compare4 = function(a,b){
	// 不用考虑王炸的情况,因为王炸后, 当前回合就结束了.
	// '单牌','对子','3张','4张' 的情况, 只需要比较第一张就可以了
	// 三带1的情况 比较第二张(牌型: aaab or bbba)
	if( a.cards.length==4 )
		return b.cards[1].getValue() > a.cards[1].getValue();
	else
		return b.cards[0].getValue()>a.cards[0].getValue();
}
// 比较两次出牌的大小
Card.compareCardType = function(a, b) {	
	// 两次出牌牌型不一样,返回错误
	if(a.typeId != b.typeId)
		return -1;
	if(a.length<=4){
		return compare4(a,b);
	}
	console.log('比牌:',b.cards[2].getValue(),a.cards[2].getValue());
	return b.cards[2].getValue()>a.cards[2].getValue();
}


